/* $RCSFile: Gtw2WireTransfer.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice.ECommerce.BankGateway/Gtw2WireTransfer.cs $
 * $log$
 * Revision 8 2010/08/09 10:07:05  christian.surieux
 *   Added Log comment to all cs files
 */

using System;
using System.Diagnostics;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Serialization;
using System.Net;
using System.IO;
using System.Globalization;

namespace AddonNice.ECommerce.Gateways.Payment
{
    /// <summary>
    /// Gateway for transactions with witrh Wire Transfert
    /// </summary>
    public class Gtw2WireTransfer : PaymentGtwBase
    {

		/// <summary>
		/// Ganeric Gateway contructor
		/// </summary>
		public Gtw2WireTransfer()
		{
		}

		/// <summary>
		/// CreditTransfer
		/// </summary>
		public override string Name
		{
			get
			{
				return "WireTransfer";
			}
		}

		/// <summary>
		/// Number identifier for gateway should be unique
		/// </summary>
		public override int TransactionTypeID
		{
			get
			{	return 1000;}
		}
		
		// paypal->txn_id  sherlock ->authorisation_id
		string _MerchantTransacID	=	string.Empty;
		public override string MerchantTransacID
		{
			get
			{	return _MerchantTransacID;
			}
		}
		public override int PendingReason
		{
			get
			{	
				return 0;
			}
		}
		// Paypal-> aucun sherlock->bank_response_code
		public override int BankResponseCode
		{
			get
			{
				return 0;
			}
				
		}
		// Paypal->first_name Sherlock->aucun
		public override string FirstName
		{
			get
			{	return string.Empty;}
		}
		// Paypal->last_name Sherlock->customer_id (we must send it)
		public override string LastName
		{
			get
			{	
				object obj	=	this["customer_id"];
				if ( obj != null && obj is string )
					return (string)obj;
				return string.Empty;
			}
		}
		
		// Paypal->payer_email Sherlock->customer_email(we must send it)
		public override string Email
		{
			get
			{	return this["customer_email"];}
		}
		
		// sherlock->card_number Paypal->aucun
		public override string CardNumber
		{
			get
			{	return  this["card_number"];}
		}

		/// <summary>
		/// This one is set in config setting->Xmldata for this merchant
		/// </summary>
		private string	_Merchant_ID	=	string.Empty;
		public string Merchant_ID
		{
			get
			{
				return _Merchant_ID;
			}
			set
			{
				if ( value != null )
					_Merchant_ID = value;
			}
		}
		

		public override void InitFromResponse(NameValueCollection Form,string custom)
		{
			base.InitFromResponse(Form,custom);
		}
		

		public override bool ShouldChangePendingStatus()
		{
			return true;
		}

		public override bool IsPaymentCompleted()
		{
			return true;
		}

		
		private string	_WireTransferUrl	=	"/EndTransac/OkFromWireTransfer.aspx";
		public string	WireTransferUrl
		{
			get
			{
				return _WireTransferUrl;
			}
			set
			{
				if ( value != null )
					_WireTransferUrl = value;
			}
		}
		/// <summary>
        /// Gets the form for contacting the Credit Institute gateway
		/// </summary>
        public override bool GetForm(Control panelSubmit,Hashtable Cust)
        {
			if ( panelSubmit == null )
			{
				if ( Gtw2TraceSwitch.TraceError )
                    Trace.WriteLine("[E]Gtw2WireTransfer GetForm panelSubmit null",Gtw2TraceSwitch.Info);
				return false;
			}
			
			if ( OrderID == Guid.Empty) 
			{
				if ( Gtw2TraceSwitch.TraceError )
                    Trace.WriteLine("[E]Gtw2WireTransfer GetForm OrderID Empty",Gtw2TraceSwitch.Info);
				return false;
			}
			
			if ( Gtw2TraceSwitch.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]Gtw2WireTransfer GetForm TransacID : {0}",TransacID),Gtw2TraceSwitch.Info);
            bool retVal		=	false;
            try
            {

				StringBuilder	s		=	new StringBuilder();
				s.AppendFormat("<form name='AddonNice2WireTransfer' action='{0}' method='post'>",WireTransferUrl);
                s.Append(GetContent(Cust));
				s.AppendFormat("<input type='submit' value='{0}'>",SubmitButtonText);
				s.Append("</form>");
				string	str				=	s.ToString();
				if ( Gtw2TraceSwitch.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]Gtw2WireTransfer GetForm TransacID: {0} , Form:\n {1}",TransacID,str),Gtw2TraceSwitch.Info);

				RawTransac				=	str;
				LiteralControl myForm	=	new LiteralControl(str);
				panelSubmit.Controls.AddAt(0,myForm);
				retVal					=	true;
            }
			catch(Exception ex)
			{
				if ( Gtw2TraceSwitch.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]Gtw2WireTransfer GetForm ex: {0}",ex),Gtw2TraceSwitch.Info);
				retVal					=	false;
			}
			return retVal;
		}

		/// <summary>
		/// Always Ok
		/// </summary>
        public override bool IsProviderOk
        {
            get
            { return true; }
        }

		/// <summary>
        /// Gets the content for contacting the Credit Institute gateway
		/// </summary>
        public override string GetContent(Hashtable Cust)
        {
			if ( Gtw2TraceSwitch.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]WireTransferUrl GetContent TransacID : {0}",TransacID),Gtw2TraceSwitch.Info);
            // We switch language, should we switch in en-US for paypal satisfaction ?????
			SetLanguage();
            string	str				=   string.Empty;
			try
			{
				
				GetCustInfo(Cust);

				StringBuilder	s		=	new StringBuilder();
				s.Append(GetHidden("custom",Gtw2Mark, true).Text);
				decimal wPrice				=	this.Price+this.Taxes+this.Expenses-this.Discount;
				if ( Gtw2TraceSwitch.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]WireTransferUrl GetContent wPrice: {0}, TransacID: {1}",wPrice,TransacID),Gtw2TraceSwitch.Info);
				s.Append(GetHidden("amount", Decimal.ToSingle(Decimal.Round(wPrice,2)).ToString("N", new System.Globalization.CultureInfo("en-US").NumberFormat),true).Text);
				s.Append(GetHidden("shipping", Decimal.ToSingle(Decimal.Round(this.ShippingPrice,2)).ToString("N", new System.Globalization.CultureInfo("en-US").NumberFormat),true).Text);
				s.Append(GetHidden("currency_code", ISOCurrencySymbol,true).Text);
				str				=	s.ToString();
				RawTransac				=	str;
				if ( Gtw2TraceSwitch.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]WireTransferUrl GetContent TransacID: {0} , Content:\n {1}",TransacID,str),Gtw2TraceSwitch.Info);
			}
			catch(Exception ex)
			{
				if ( Gtw2TraceSwitch.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]WireTransferUrl GetContent ex : {0}",ex),Gtw2TraceSwitch.Info);
                throw;
			}
			finally
			{
				RestLanguage();
			}
			return str;
        }
    }
}
